package com.netty.example.nio;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class NIOTest4 {
    public static void main(String[] args) throws Exception {
        FileInputStream fileInputStream = new FileInputStream("input.txt");
        FileOutputStream fileOutputStream = new FileOutputStream("output.txt");
        FileChannel inputChannel = fileInputStream.getChannel();
        FileChannel outputChannel = fileOutputStream.getChannel();
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        while (true) {
            buffer.clear(); // 如果注释掉该行代码会发生什么情况？
            System.out.println("buffer limit:" + buffer.limit());
            System.out.println("buffer position: " + buffer.position());
            int read = inputChannel.read(buffer);
            System.out.println("read: " + read);
            if (read == -1) break;
            buffer.flip();
            outputChannel.write(buffer);
        }
        System.out.println("input to output success");
        inputChannel.close();
        outputChannel.close();
        fileInputStream.close();
        fileOutputStream.close();
    }
}
